﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.Utility;
using EnvironmentManagerSystem.BLL;
namespace EnvironmentManagerSystem.HuanTong
{
    public partial class HTUserManager : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                //是否是管理员并操作isAdmin
                if (!(Boolean)Session["gly"])
                {
                    gv_HTUserManager.Columns[gv_HTUserManager.Columns.Count - 1].Visible = false;
                    divInsert.Visible = false;
                }
                Bindgv_HTUserManager();
                ddlUserTypes();
            }
        }

        #region 绑定(用户列表、用户角色)(x)
        //用户列表
        public void Bindgv_HTUserManager()
        {
            gv_HTUserManager.DataSource = ren.GetUser((Boolean)Session["gly"], hidUserName.Value, hidUserType.Value);
            gv_HTUserManager.DataBind();
        }
        //用户角色
        private void ddlUserTypes()
        {
            ddlUserType.DataSource = Role.GetLists();
            ddlUserType.DataTextField = "RoleName";
            ddlUserType.DataValueField = "ID";
            ddlUserType.DataBind();
            this.ddlUserType.Items.Insert(0, new ListItem("请选择", "请选择"));
        }
        #endregion

        #region 按钮(刷新数据、重置按钮、查询按钮)(x)
        //刷新数据
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtUserName.Text = string.Empty;
            ddlUserType.SelectedIndex = 0;
        }
        //重置按钮
        protected void Button3_Click(object sender, EventArgs e)
        {
            Bindgv_HTUserManager();
            txtUserName.Text = "";
            ddlUserType.SelectedIndex = 0;
            txtUserName.Focus();
        }
        //查询按钮
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            hidUserName.Value = txtUserName.Text.Trim();
            if (ddlUserType.SelectedIndex != 0)
                hidUserType.Value = ddlUserType.SelectedItem.Text;
            else
                hidUserType.Value = string.Empty;
            Bindgv_HTUserManager();
        }
        #endregion

        #region GridView事件(分页、分页后、编辑、取消、更新、删除)(x)
        //分页事件
        protected void gv_HTUserManager_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv_HTUserManager.PageIndex = e.NewPageIndex;
        }
        //分页后事件
        protected void gv_HTUserManager_PageIndexChanged(object sender, EventArgs e)
        {
            Bindgv_HTUserManager();
        }
        //编辑事件
        protected void gv_HTUserManager_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv_HTUserManager.EditIndex = e.NewEditIndex;
            Bindgv_HTUserManager();
        }
        //取消事件
        protected void gv_HTUserManager_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv_HTUserManager.EditIndex = -1;
            Bindgv_HTUserManager();
        }
        //更新事件
        protected void gv_HTUserManager_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Check(e.RowIndex))
            {
                renData re = new renData();
                re.ID1 = Convert.ToInt32(gv_HTUserManager.Rows[e.RowIndex].Cells[0].Text);
                re.Username = ((TextBox)gv_HTUserManager.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
                re.Logname = ((TextBox)gv_HTUserManager.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
                re.Pwd = ((TextBox)gv_HTUserManager.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")).Text.Trim();
                re.Phone = ((TextBox)gv_HTUserManager.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
                re.Zhineng = gv_HTUserManager.Rows[e.RowIndex].Cells[5].Text;
                renDataCollection list = ren.GetRen(re.ID1.ToString());
                if (list.Count > 0)
                {
                    string UN = list[0].Username.ToString();
                    string LN = list[0].Logname.ToString();
                    if (ren.UpdateUsers(re.Username, re.Logname, re.Pwd, re.Phone, UN, LN)) { }
                }
                if (ren.UpdateUser(re))
                {
                    gv_HTUserManager.EditIndex = -1;
                    Bindgv_HTUserManager();
                    Jscript.Alert("修改成功", this);
                }
                else
                {
                    Jscript.Alert("用户名或登录名已存在", this);
                }
            }
        }
        //删除事件
        protected void gv_HTUserManager_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string a = gv_HTUserManager.DataKeys[e.RowIndex].Value.ToString();
            renDataCollection list = ren.GetRen(a);
            if (list.Count > 0)
            {
                string UNS = list[0].Username.ToString();
                string LNS = list[0].Logname.ToString();
                if (ren.DeleteUsers(UNS, LNS) > 0) { }
            }
            if (ren.DeleteUser(gv_HTUserManager.DataKeys[e.RowIndex].Value.ToString()) > 0)
            {
                Jscript.Alert("删除成功", this);
                Bindgv_HTUserManager();
            }
            else
            {
                Jscript.Alert("删除失败", this);
            }
        }
        #endregion

        #region 鼠标滑动(x)
        protected void gv_HTUserManager_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            OfficeHelper.HelpDv(e.Row);
        }
        //鼠标滑动
        protected void gv_HTUserManager_DataBound(object sender, EventArgs e)
        {
            OfficeHelper.HelpDv(gv_HTUserManager);
        }
        #endregion

        #region 验证文本框(x)
        public Boolean Check(int i)
        {
            if (
                !((TextBox)gv_HTUserManager.Rows[i].Cells[1].Controls[0]).Text.Trim().Equals("")
                && !((TextBox)gv_HTUserManager.Rows[i].Cells[2].Controls[0]).Text.Trim().Equals("")
                && !((TextBox)gv_HTUserManager.Rows[i].Cells[3].FindControl("TextBox1")).Text.Trim().Equals("")
                && !((TextBox)gv_HTUserManager.Rows[i].Cells[4].Controls[0]).Text.Trim().Equals(""))
            {
                if (Regex.IsMatch(((TextBox)gv_HTUserManager.Rows[i].Cells[4].Controls[0]).Text.Trim(), @"^[0-9]*$"))
                {
                    return true;
                }
                else
                {
                    Jscript.Alert("电话号码输入错误", this);
                    return false;
                }
            }
            else
            {
                Jscript.Alert("用户名,登录名,密码,电话都不能为空", this);
                return false;
            }
        }
        #endregion
    }
}
